package com.bluestone.scripts;

import java.io.File;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

import com.bluestone.util.Util;

public class LibraryTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		//generateAIVRLibrary();

	}
	public static void generateLibrary(){
		try {
			Library library = new Library();
			library.setName("test");

			
			JAXBContext context = JAXBContext.newInstance(library.getClass());
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty("jaxb.formatted.output",new Boolean(true));
			
			// create parent folder if it doesn't exist to avoid IOException
			String path =  "./scripts/library.xml";
			File file = new File(path);
			if ( file.getParentFile()!=null && !file.getParentFile().exists() ) {
				file.getParentFile().mkdirs();
			}	
			marshaller.marshal(library, file);
			
		}catch(Exception me) {
			Util.getLogger().error("fail to save library "+me);
		}			
	}	
	
	public static void generateAIVRLibrary(){
		try {
			String script = "";
			Library library = new Library();
			library.setName("AIVRVoexoLib");
          
			Function dialFuntion = new Function();
			dialFuntion.setName("dial");
			script = "<![CDATA[\n";
			script = script + "<action type=\"altkeydown\"/>\n";
			script = script + "<action value=\"d\" type=\"inputkey\"/>\r\n";
			script = script + "<action value=\"d\" type=\"altkeyup\"/>\r\n";
			script = script + "]]>";
			dialFuntion.setScript(script);
			library.putFunction(dialFuntion);
			
			JAXBContext context = JAXBContext.newInstance(library.getClass());
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty("jaxb.formatted.output",new Boolean(true));
			
			// create parent folder if it doesn't exist to avoid IOException
			String path =  "./scripts/aivr_voexolib.xml";
			File file = new File(path);
			if ( file.getParentFile()!=null && !file.getParentFile().exists() ) {
				file.getParentFile().mkdirs();
			}	
			marshaller.marshal(library, file);
			
		}catch(Exception me) {
			Util.getLogger().error("fail to save library "+me);
		}			
	}		

}
